<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1100" destroy-on-close @closed="$emit('closed')" align-center
             :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <!-- <el-col :span="8">
                    <el-form-item label="BOM单编号" prop="bom_code">
                        <el-input v-model="form.bom_code" readonly placeholder="自动生成" />
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="BOM分类" prop="cate_id">
                        <el-select v-model="form.cate_id" placeholder="请选择分类" style="width:100%" clearable filterable>
                            <el-option v-for="item in cateList" :key="item.id" :label="item.cate_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="商品名称" prop="goods_id">
                        <el-input v-model="goods_name" placeholder="请选择商品" @click="checkGoods(1)" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="物料编码">
                        <el-input v-model="mater_code" placeholder="物料编码" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="商品编码">
                        <el-input v-model="goods_code" placeholder="商品编码" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="商品规格">
                        <el-input v-model="goods_spec" placeholder="商品规格" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="24">
                <el-form-item label="设置物料" prop="bom_child">
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="checkGoods(2)">选择商品</el-button>
                    <sc-form-table ref="table" v-model="form.bom_child" :addTemplate="addTemplate" placeholder="暂无数据" drag-sort height="250" hideAdd>
                        <el-table-column prop="mater_code" label="物料编码" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.mater_code" placeholder="物料编码" disabled></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_code" label="商品编码" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.goods_code" placeholder="商品编码" disabled></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_name" label="商品名称" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.goods_name" placeholder="商品名称" disabled></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_spec" label="商品规格" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.goods_spec" placeholder="商品规格" disabled></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="number" label="数量" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" placeholder="数量"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit_name" label="单位" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.unit_name" placeholder="单位" disabled></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsupdate" @closed="dialog.goods=false" :query="{system_disable:0}"></select-goods>
</template>

<script>
import selectGoods from '../goods/info/selectGoods.vue';
export default {
    components: { selectGoods },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增BOM',
				edit: '编辑BOM',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                goods_id: "",
                cate_id: "",
                bom_child: [],
			},
			//验证规则
			rules: {
				goods_id: [{ required: true, message: '请选择商品' }],
				cate_id: [{ required: true, message: '请选择bom分类' }],
			},
            goods_name: "",
            mater_code: "",
            goods_code: "",
            goods_spec: "",
            addTemplate:{},
            dialog:{
                goods: false,
            },
            cateList: []
		}
	},
	mounted() {
        this.getCate()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.bom.edit.post(this.form);
					} else {
						res = await this.$API.goods.bom.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.goods.bom.detail.get({id:data.id});
            console.log(res);
            this.form.goods_id = res.data.goods_id
            this.goods_name = res.data.goods.goods_name
            this.mater_code = res.data.goods.material_code
            this.goods_code = res.data.goods.goods_code
            this.goods_spec = res.data.goods.goods_spec
            this.form.cate_id = res.data.cate_id
            let array = []
            res.data.child_all.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    number:item.number,
                    goods_name:item.goods && item.goods.goods_name,
                    goods_spec:item.goods && item.goods.goods_spec,
                    unit_name:item.goods && item.goods.unit_name,
                    mater_code:item.goods && item.goods.material_code,
                    goods_code:item.goods && item.goods.goods_code,
                })
            })
            this.form.bom_child = array
		},
        onFocus(event){
            event.target.select();
        },
        // 选择商品
        checkGoods(e){
            this.dialog.goods = true
            this.$nextTick(() => {
                this.$refs.goodsDialog.open('add').setData(e)
            })
        },
        goodsupdate(data,type){
			console.log('data',data)
            if(type == 2){
                let array = []
                data.map((item)=>{
                    array.push({
                        goods_id:item.id,
                        goods_name:item.goods_name,
                        goods_spec:item.goods_spec,
                        unit_name:item.unit.unit_name,
                        mater_code:item.material_code,
                        goods_code:item.goods_code,
                    })
                })
                let existingIds = this.form.bom_child.map(item => item.goods_id);
                for(let i = array.length - 1; i >= 0; i--){
                    if(existingIds.includes(array[i].goods_id)){
                        array.splice(i, 1);
                    }
                }
                this.form.bom_child = [...this.form.bom_child,...array]
            }else{
                this.form.goods_id = data[0].id
                this.goods_name = data[0].goods_name
                this.mater_code = data[0].material_code
                this.goods_code = data[0].goods_code
                this.goods_spec = data[0].goods_spec
            }
        },
        // 获取分类
        async getCate(){
            var res = await this.$API.goods.bomCate.all.get();
            this.cateList = res.data
        }
	}
}
</script>

<style></style>
